package org.car.world.controller;

import javax.servlet.http.HttpSession;


import org.car.world.common.util.Page;
import org.car.world.core.entily.AdminTable;
import org.car.world.core.entily.UserTable;
import org.car.world.service.AdminService;
import org.car.world.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.multipart.MultipartFile;
import io.lettuce.core.output.StreamingChannel;

/**
 * @author Administrator
 * @ClassName: UserController
 * @Description: 用户管理的Controller
 * @date Nov 6, 2018
 */
@Controller
@RequestMapping("admin")
public class UserController {

    @Autowired
    AdminService adminService;

    @Autowired
    UserService userService;

    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: login_init
     * @Description: TODO(访问登陆)
     */
    @RequestMapping("/")
    public String login_init() {
        return "user_login";
    }

    @RequestMapping("/lo")
    public String lo_init() {
        return "user_login";
    }

    /**
     * @param @param  username
     * @param @param  password
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: dologin_method
     * @Description: 登陆验证方法
     */
    @PostMapping("dologin")
    public String dologin_method(String username, String password, HttpSession session) {
        AdminTable adminTable = new AdminTable();
        adminTable.setAdminLogname(username);
        adminTable.setAdminPwd(password);
        if (adminService.login(adminTable) != null) {
            AdminTable adminTable1 = adminService.login(adminTable);
            session.setAttribute("Admin", adminTable1);
            return "index";
        } else {
            return "redirect:lo";
        }
    }


    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: doindex_method
     * @Description: 访问首页
     */
    @RequestMapping("doindex")
    public String doindex_method() {
        return "index";
    }

    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: userlist_method
     * @Description: 访问用户管理
     */
    @RequestMapping("douserlist")
    public String userlist_method(@RequestParam(value = "form", required = false, defaultValue = "1") Integer form, Model model) {
        Page<UserTable> page = new Page<UserTable>();
        page.setCurrentPage(form);
        page.setPageSize(2);
        page.setTotalRow(userService.selectcount());
        page.setPageList(userService.selectLimit(page.getCurrentPage(), page.getPageSize()));
        model.addAttribute("page", page);
        return "user_list";
    }

    @RequestMapping("toupdateuser")
    public String updateUser(Integer userId, Model model) {
        UserTable user = userService.selectByPrimaryKey(userId);
        System.out.println(userId);
        model.addAttribute("user", user);
        return "user_up";
    }

    @RequestMapping("doupdateuser")
    public String doUpdateUser(UserTable user) {
        int result = userService.updateByPrimaryKey(user);
        if (result > 0) {
            return "redirect:douserlist";
        }
        return "redirect:toupdateuser";
    }

    @ResponseBody
    @GetMapping("deluser")
    public Boolean doDeluser(int userId) {
        int result = userService.deleteByPrimaryKey(userId);
        if (result > 0) {
            return true;
        }
        return false;
    }


}
